package com.zdyt.opc.common;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.zdyt.opc.entity.OpcData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.core.JIVariant;
import org.openscada.opc.lib.da.DataCallback;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.ItemState;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.text.DecimalFormat;
import java.util.Map;


// 数据采集发送器  通过该对象传输至目标服务器
@Data
@Slf4j
public class DataSampAndSender implements DataCallback {

    private RestTemplate restTemplate;

    private String targetUrl;

    private Map<String, String> itemId2CN;
    private String title;
    private String bgColor;

    public DataSampAndSender(RestTemplate restTemplate, String targetUrl,
                             Map<String, String> itemId2CN, String title) {
        this.restTemplate = restTemplate;
        this.targetUrl = targetUrl;
        this.itemId2CN = itemId2CN;
        this.title = title;
    }

    @Override
    public void changed(Item item, ItemState itemState) {

        OpcData opcData = convertToEntity(item, itemState);

        if (opcData != null){
            log.info("采集数据:【{}】",opcData.toString());

            // TODO restTemplate
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String jsonData = objectMapper.writeValueAsString(opcData);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE));
                headers.add("Accept",MediaType.APPLICATION_JSON.toString());
                HttpEntity<String> entity = new HttpEntity<>(jsonData, headers);
                String result = restTemplate.postForObject(targetUrl, entity, String.class);
                if ("success".equals(result)){
                    log.info("数据发送成功:【{}】, 目标地址:【{}】",opcData.toString(),targetUrl);
                }else {
                    log.error("数据发送失败【{}】,目标地址:【{}】",opcData.toString(),targetUrl);
                }

            } catch (Exception e) {
                log.error("数据发送失败【{}】,目标地址:【{}】",opcData.toString(),targetUrl);
            }

        }

    }

    public OpcData convertToEntity(Item item, ItemState itemState){
        log.info("转换数据！");
        OpcData result = null;
        String itemId = item.getId();
        String cnName = itemId2CN.get(itemId);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        try {
            Object value = null;
            int type = itemState.getValue().getType();

            // float
            if (type == JIVariant.VT_R4){
                value = itemState.getValue().getObjectAsFloat();
                value = decimalFormat.format(value);
            }

            // double
            if (type == JIVariant.VT_R8){
                value = itemState.getValue().getObjectAsDouble();
                value = decimalFormat.format(value);
            }

            // short
            if (type == JIVariant.VT_I2){
                value = itemState.getValue().getObjectAsShort();
            }

            // 无符号整形
            if (type == JIVariant.VT_UI2){
                value = itemState.getValue().getObjectAsUnsigned().getValue();
            }

            if (type == JIVariant.VT_BSTR){
                value = itemState.getValue().getObjectAsString();
            }

            if (value != null){
                result = new OpcData(itemId,value,cnName,title);
            }

        } catch (JIException e) {
            log.error(e.getMessage());
        }

        return result;
    }

}
